#ifndef _MODULE_CONSOLE
#define _MODULE_CONSOLE
#include "page.module.js"
#include "prefs.module.js"
#include "boss.module.js"
#include "mwcaptcha.module.js"
#include "log.lib.js"
#include "utility.lib.js"
#include "usolib.lib.js"

var script_console=<><![CDATA[
<div class="generic_dialog_popup" style="top: 50px; width:600px !important;">
	<table id="pop_dialog_table" class="pop_dialog_table" style="width:600px !important;">
	<tbody>
		<tr>
			<td class="pop_topleft"></td>
			<td class="pop_border pop_top"></td>
			<td class="pop_topright"></td>
		</tr>
		<tr>
			<td class="pop_border pop_side"></td>
			<td id="pop_content" class="pop_content">
				<h2 class="dialog_title" style="position:relative;"><span>MWTurbo Console</span>
					<img id="mwt_working" style="display:none; position: absolute; top: 6px; right: 6px;" src="http://www.mwturbo.com/work.gif" />
				</h2>
				<div class="dialog_content">
					<div class="dialog_body">
						<h3 class="mwt_pref_title" style="margin-top:0;">Stats</h3>
						<table class="mwt_stats" style="width:100%;border:none:padding:0;margin:0;">
						<tbody>
							<tr>
								<td width="50%">Cash: <span id="mwt_cash">$0</span></td>
								<td width="50%">Health: <span id="mwt_health">0/0</span></td>
							</tr>
						<table class="mwt_stats" style="width:100%;border:none:padding:0;margin:0;">
						<tbody>
							<tr>
								<td width="50%">Energy: <span id="mwt_energy">0/0</span></td>
								<td width="50%">Stamina: <span id="mwt_stamina">0/0</span></td>
							</tr>
						<table class="mwt_stats" style="width:100%;border:none:padding:0;margin:0;">
						<tbody>
							<tr>
								<td width="50%">Total Damage Dealt: <span id="mwt_damaged">0</span></td>
								<td width="50%">Total Damage Recieved: <span id="mwt_damager">0</span></td>
							</tr>
						</tbody>
						</table>
						<table class="mwt_stats" style="width:100%;border:none:padding:0;margin:0;">
						<tbody>
							<tr>
								<td width="50%">Total $ Won: <span id="mwt_won">$0</span></td>
								<td width="50%">Total Exp Gain: <span id="mwt_exp">0</span></td>
							</tr>
						</tbody>
						</table>
						<table class="mwt_stats" style="width:100%;border:none:padding:0;margin:0;">
						<tbody>
							<tr>
								<td width="50%">Total Bounties: <span id="mwt_bount">0</span></td>
								<td width="50%">Total Wins: <span id="mwt_wins">0</span></td>
							</tr>
						</tbody>
						</table>
						<h3 class="mwt_pref_title">Status</h3>
						<table id="mwt_status" style="width:100%;border:none:padding:0;margin:0;">
						<tbody>
							<tr>
								<td class="script_name"><span id="mwt_hitlist">Hitlist</span></td>
								<td id="status_hitlist">Click to begin</td>
							</tr>
							<tr>
								<td class="script_name"><span id="mwt_revenge">Revenge</span></td>
								<td id="status_revenge">Click to begin</td>
							</tr>
							<tr>
								<td class="script_name"><span id="mwt_fight">Fight</span></td>
								<td id="status_fight">Click to begin</td>
							</tr>
							<tr>
								<td class="script_name"><span id="mwt_job">Job</span></td>
								<td id="status_job">Click to begin</td>
							</tr>
							<tr>
								<td class="script_name"><span id="mwt_property">Property</span></td>
								<td id="status_property">Click to begin</td>
							</tr>
							<tr>
								<td class="script_name"><span id="mwt_defend">Defend</span></td>
								<td id="status_defend">Click to begin</td>
							</tr>
						</tbody>
						</table>
						<h3 class="mwt_pref_title">Log <span style="font-size:10px;"><a id="mwtc_lv" href="#" onclick="return false;">[Log Viewer]</a> <a id="mwtc_clearlog" href="#" onclick="return false;">[Clear]</a></span></h3>
						<select multiple="multiple" id="mwt_logger" style="width:100%;height:120px;"></select>
						<div id="mwt_captcha_box" style="display:none;"><h3 class="mwt_pref_title">Captcha Found!</h3>
						<img id="captcha_image" style="display: block;float:left;" src="" alt="captcha" />
						<input id="captcha_answer" style="display: block;float:left;width: 150px;margin:18px 0 0 15px;" type="text" />
						<input id="captcha_submit" style="display: block;float:left;margin: 17px 0pt 0pt 15px;" value="Submit" name="save" class="inputsubmit" type="button">
						<div style="clear:both;"></div></div>
						<div id="mwc_captcha_box" style="display:none;"><h3 class="mwt_pref_title">You have a MWcaptcha to solve!</h3>
						<img id="mwcaptcha_image" style="display: block;float:left;" src="" alt="captcha" />
						<input id="mwcaptcha_answer" style="display: block;float:left;width: 150px;margin:18px 0 0 15px;" type="text" />
						<input id="mwcaptcha_submit" style="display: block;float:left;margin: 17px 0pt 0pt 15px;" value="Submit" name="save" class="inputsubmit" type="button">
						<div style="clear:both;"></div></div>
					</div>
					<div class="dialog_buttons">
						<input id="mwtc_close" value="Shut Down" name="save" class="inputsubmit" type="button">
					</div>
				</div>
			</td>
			<td class="pop_border pop_side"></td>
		</tr>
		<tr>
			<td class="pop_bottomleft"></td>
			<td class="pop_border pop_bottom"></td>
			<td class="pop_bottomright"></td>
		</tr>
	</tbody>
	</table>
</div>
]]></>.toString();

var script_announce=<><![CDATA[
<div class="generic_dialog_popup" style="top: 95px;">
	<table id="pop_dialog_table" class="pop_dialog_table">
	<tbody>
		<tr>
			<td class="pop_topleft"></td>
			<td class="pop_border pop_top"></td>
			<td class="pop_topright"></td>
		</tr>
		<tr>
			<td class="pop_border pop_side"></td>
			<td id="pop_content" class="pop_content">
				<h2 class="dialog_title"><span id="a_title"></span></h2>
				<div class="dialog_content">
					<div id="a_content" class="dialog_body"></div>
					<div class="dialog_buttons">
						<input id="mwta_close" value="Close" name="cancel" class="inputsubmit inputaux" type="button">
					</div>
				</div>
			</td>
			<td class="pop_border pop_side"></td>
		</tr>
		<tr>
			<td class="pop_bottomleft"></td>
			<td class="pop_border pop_bottom"></td>
			<td class="pop_bottomright"></td>
		</tr>
	</tbody>
	</table>
</div>
]]></>.toString();

var Console = {
	a_timer: null,
	visable: false,
	toggle: function() {
		if (this.visable) {
			Page.halt();
			Mwcaptcha.stop = true;
			$('#mwt_console_pop').animate({
				opacity: "0"
			},
			100, function() {
				$(this).remove();
			});
			this.visable = false;
			return;
		}
		var div = document.createElement('div');
		div.setAttribute('style', 'z-index:99 !important;');
		div.id = "mwt_console_pop";
		div.className = "generic_dialog pop_dialog";
		div.innerHTML = script_console;
		document.body.insertBefore(div, document.body.lastChild);
		var div = document.createElement('div');
		div.setAttribute('style', 'z-index:101 !important;display:none;');
		div.id = "mwt_a";
		div.className = "generic_dialog pop_dialog";
		div.innerHTML = script_announce;
		document.body.insertBefore(div, $('#mwt_console_pop')[0]);
		$('#mwtc_close').bind('click', function() {
			Console.toggle();
		});
		$("//table[@id='mwt_status']//span").bind('click', function() {
			Page.toggle(this.id.match(/mwt_(.+)/)[1]);
		});
		$('#mwtc_lv').bind('click', function() {
			Log.show();
		});
		$('#mwtc_clearlog').bind('click', function() {
			document.getElementById('mwt_logger').textContent = '';
			Log.memory = [];
			alert('Log Memory Cleared!');
		});
		$('#captcha_submit').bind('click', function() {
			Page.captcha_submit();
		});
		$('#captcha_answer').bind('keyup', function(event) {
			if (event.keyCode == 13) Page.captcha_submit();
		});
		$('#mwcaptcha_answer').bind('keyup', function(event) {
			if (event.keyCode == 13) Page.mwcaptcha_submit();
		});
		$('#mwcaptcha_submit').bind('click', function() {
			Page.mwcaptcha_submit();
		});
		$('#mwta_close').bind('click', Console.a_close);
		Boss.update(document.body.innerHTML);
		this.visable = true;
		if (Settings.mwcaptcha === true && Settings.mwc_earn === true) {
			Mwcaptcha.stop = false;
			if (Mwcaptcha.solving == false) {
				Mwcaptcha.solving = true;
				Mwcaptcha.solve();
			}
		}
		return;
	},
	log: function(details) {
		if (!details)
			return;

		var time = new Date(),
			min = time.getMinutes(),
			seconds = time.getSeconds(),
			h = "",
			option = document.createElement('option'),
			log = $('#mwt_logger')[0];

		if (10 > min)
			min = '0' + min;
		if (10 > seconds)
			seconds = '0' + seconds;

		details.push({
			name: 'Time',
			value: time
		});
		Log.memory.push(details);

		for (var i = 0, il = details.length; i < il; i++) {
			if (details[i].name == "Time")
				h += 'Time: ' + time.getHours() + ':' + min + ':' + seconds;
			else
				h += details[i].name + ": " + details[i].value + "; ";
		}
		option.appendChild( document.createTextNode( h ) );
		log.insertBefore( option, log.firstChild );

		if ( log.options.length >= 100 )
			log.options[ log.options.length - 1 ].parentNode.removeChild( log.options[ log.options.length - 1 ] );
		log.selectedIndex = 0;
	},
	clear: function() {
		$('#mwt_logger').html('');
	},
	status: function(script, str) {
		$('#status_' + script).html(str);
	},
	update_stats: function() {
		$('#mwt_cash').html(Boss.cash_str);
		$('#mwt_health').html(Math.floor(Boss.health * 100) + '%');
		$('#mwt_energy').html(Boss.energy[0] + '/' + Boss.energy[1]);
		$('#mwt_stamina').html(Boss.stamina + '/' + Boss.stamina_total);
	},
	announce: function(details) {
		$('#a_title').html(details.title);
		$('#a_content').html(details.content);
		$('#mwt_a').show();
		if (Console.a_timer) {
			Console.a_timer.stop();
		}
		Console.a_timer = new Utility.a_timer({
			time: 5,
			message: 'Close [[time]]',
			fn: Console.a_close
		});
		Console.a_timer.start();
	},
	a_close: function() {
		if (Console.a_timer) {
			Console.a_timer.stop();
		}
		$('#mwt_a').hide();
		if (Page.special_pause === true) {
			Page.resume();
		}
	}
};
#endif
